/*
Longest Common Prefix
Write a function to find the longest common prefix string amongst an array of strings.
*/

#include <iostream>
#include <vector>
#include <algorithm>
#include<string>
#include "print.h"
using namespace std;

class Solution {
public:
	string longestCommonPrefix(vector<string> &strs) {

		if (strs.size() == 0)
			return "";

		string minString = strs[0];
	
		int minLen, prefix;

		for (int i = 0; i < strs.size(); i++){
			
		    prefix = 0;
			string curString = strs[i];
		
			minLen = curString.size() < minString.size() ? curString.size() : minString.size();

			if (curString[0] != minString[0])
			{
				return  "";
			}

			for (int j = 0; j < minLen; j++)
			{
				if (curString[j] == minString[j])
				{
					prefix++;
				}
				
			}
			minString = minString.substr(0, prefix);
		}
		return minString.substr(0,prefix);
	}
};

int main(int argc, char* argv[])
{
	string str;
	vector<string> vecStr;
	//str = "abc";
	//str a = str[0];

	Solution s;

	for (int i = 1; i < argc; i++){
		cout << argv[i] << endl;
		//a = atoi(argv[i]);
		str.assign(argv[i], strlen(argv[i]));
		vecStr.push_back(str);
		


	}
	printVectStr(vecStr);
	cout << endl;
	cout << "The longest common Prefix" << endl;
	cout << s.longestCommonPrefix(vecStr) << endl;
	system("pause");
	return 0;
}